#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > rsp_exci_cmfun.info <<'%EOF%'
   rsp_exci_cmfun
   --------------
   Molecule:         LiH
   Wave Function:    MCSCF (CAS) / 4-31G + .CM FUN Rydberg functions
   Test Purpose:     Excitation energy with extra Rydberg basis functions
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > rsp_exci_cmfun.mol <<'%EOF%'
BASIS
4-31G
Calculation of excitation energy

    3    2  X  Y    1 1.00D-12
        1.0   1
H           .0000000000             .0000000000            2.0969699107
        3.0   1
Li          .0000000000             .0000000000            -.9969699107
! and the center for the Rydberg functions:
        0.0   1
X           .0000000000             .0000000000             .0000000000
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > rsp_exci_cmfun.dal <<'%EOF%'
**DALTON INPUT
.RUN RESPONS
*MOLBAS
.CM FUN
  2 3 4
**WAVE FUNCTIONS
.HF
.MP2
.MCSCF
*SCF INPUT
.DOUBLY OCCUPIED
 2 0 0 0
*CONFIGURATION INPUT
.INACTIVE
 1 0 0 0
.ELECTRONS
 2
.CAS SPACE
 2 0 0 0
.SYMMET
 1
.SPIN MULT
 1
**RESPONS
*LINEAR
.DIPLEN
.SINGLE
.ROOTS
 3 1 0 0
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >rsp_exci_cmfun.check
cat >>rsp_exci_cmfun.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

if $GREP -q "not implemented for parallel calculations" $log; then
   echo "TEST ENDED AS EXPECTED"
   exit 0
fi

# Basis set
CRIT1=`$GREP "H * 1 * 1\.0000 * 4 * 2 * \[4s\|2s\]" $log | wc -l`
CRIT2=`$GREP "Li * 1 * 3\.0000 * 17 * 9 * \[8s3p\|3s2p\]" $log | wc -l`
CRIT3=`$GREP "total\: * 3 * 4\.0000 * 48 * 38" $log | wc -l`
TEST[1]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[1]=3
ERROR[1]="BASIS SET NOT READ CORRECTLY"

# Geometry
CRIT1=`$GREP "Total number of coordinates\: * 9" $log | wc -l`
CRIT2=`$GREP "1 * x * 0*\.0000000000" $log | wc -l`
CRIT3=`$GREP "2 * y * 0*\.0000000000" $log | wc -l`
CRIT4=`$GREP "3 * z * 2\.0969699107" $log | wc -l`
CRIT5=`$GREP "4 * x * 0*\.0000000000" $log | wc -l`
CRIT6=`$GREP "5 * y * 0*\.0000000000" $log | wc -l`
CRIT7=`$GREP "6 * z * \-0*\.9969699107" $log | wc -l`
TEST[2]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7`
CTRL[2]=7
ERROR[2]="GEOMETRY NOT READ CORRECTLY"

# Symmetry
CRIT1=`$GREP "Number of coordinates in each symmetry\: * 3 * 3 * 3 * 0" $log | wc -l`
CRIT2=`$GREP "Number of orbitals in each symmetry\: * 19 * 8 * 8 * 3" $log | wc -l`
TEST[3]=`expr	$CRIT1 \+ $CRIT2`
CTRL[3]=2
ERROR[3]="SYMMETRY NOT CORRECT"

# Energies
CRIT1=`$GREP "Hartree\-Fock total energy *\: * \-7\.9780508" $log | wc -l`
CRIT2=`$GREP "\= MP2 second order energy *\: * \-7\.991125" $log | wc -l`
CRIT3=`$GREP "Final MCSCF energy\: * \-7\.9947773" $log | wc -l`
TEST[4]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[4]=3
ERROR[4]="ENERGIES NOT CORRECT"

# Response setup
CRIT1=`$GREP "Linear Response single residue calculation" $log | wc -l`
CRIT2=`$GREP "3 Excitation energies are calculated for symmetry no\. * 1" $log | wc -l`
CRIT3=`$GREP "1 property residues are calculated with labels\:" $log | wc -l`
CRIT4=`$GREP "1 Excitation energies are calculated for symmetry no\. * 2" $log | wc -l`
TEST[5]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[5]=5
ERROR[5]="RESPONSE CALCULATION NOT SET UP CORRECTLY"

# Excitation energy (ZDIPLEN)
CRIT1=`$GREP "\@ *STATE NO\: * 2 \*TRANSITION MOMENT\: * \-*0\.407.* \*ENERGY\(eV\)\: * 5\.497" $log | wc -l`
CRIT2=`$GREP "\@ *Excitation energy \: * 0*\.202.* au" $log | wc -l`
CRIT3=`$GREP "\@ *Total energy \: * \-7\.792.* au" $log | wc -l`
CRIT4=`$GREP "\@ *Oscillator strength \(LENGTH\) * \: * 2\.23.*E\-02" $log | wc -l`
TEST[6]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 `
CTRL[6]=4
ERROR[6]="EXCITATION ENERGY (ZDIPLEN) NOT CORRECT"

# Excitation energy (XDIPLEN)
CRIT1=`$GREP "\@ *STATE NO\: * 1 \*TRANSITION MOMENT\: * \-*1\.540.* \*ENERGY\(eV\)\: * 4\.209" $log | wc -l`
CRIT2=`$GREP "\@ *Excitation energy \: * 0*\.154.* au" $log | wc -l`
CRIT3=`$GREP "\@ *Total energy \: * \-7\.840.* au" $log | wc -l`
CRIT4=`$GREP "\@ *Operator label\: * XDIPLEN " $log | wc -l`
CRIT5=`$GREP "\@ *Oscillator strength \(LENGTH\) * \: * 0*\.244" $log | wc -l`
TEST[7]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5`
CTRL[7]=5
ERROR[7]="EXCITATION ENERGY (XDIPLEN) NOT CORRECT"

PASSED=1
for i in 1 2 3 4 5 6 7
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} ); "
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
